package day09;

public class User implements Cloneable{
    private int id;
    private String username;
    private int age;
    private UserInfo userInfo;

    public User(){
        System.out.println("User...");
    }

    public User(int id, String username) {
        this.id = id;
        this.username = username;
    }

    public User(int id, String username, int age) {
        this.id = id;
        this.username = username;
        this.age = age;
        System.out.println("带参构造...");
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    @SuppressWarnings("all")
    @Override
    public boolean equals(Object o){
        System.out.println("equals...");
        if(this == o)return true;
        if(o == null || getClass()!= o.getClass())return false;

        User user = (User) o;
        if(id != user.id)return false;
        return username != null?username.equals(user.username):user.username == null;

    }
    @Override
    public int hashCode(){
        int result = id;
        result = 31 * result+(username!=null?username.hashCode():0);
        System.out.println("哈希值:"+result);
        return result;
    }

    @Override
    public Object clone() throws CloneNotSupportedException{
        User copyUser = new User();

        copyUser.setId(id);
        copyUser.setUsername(username);
        copyUser.setAge(age);

        if(null!=userInfo){
            UserInfo copyInfo = new UserInfo();
            copyInfo.setId(userInfo.getId());
            copyInfo.setInfo(userInfo.getInfo());
            copyUser.setUserInfo(copyInfo);
        }
        return copyUser;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", age=" + age +
                ", userInfo=" + userInfo +
                '}';
    }
}
